<!-- 赛事列编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改体测列':'添加体测列'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">



      <el-form-item
        label="体测名称:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入体测名称"
          clearable/>
      </el-form-item>

      <el-form-item label="年级(选填):" >
       <el-select
         filterable
         clearable
         v-model="form.gradeId"
         @change="$forceUpdate()"
         size="small"
         placeholder="-请选择年级-"
         class="ele-block">
         <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.id"/>
       </el-select>
      </el-form-item>
    <el-form-item label="学院(选填):" >
       <el-select
         filterable
         clearable
         v-model="form.departmentsId"
         @change="$forceUpdate()"
         size="small"
         placeholder="-请选择学院-"
         class="ele-block">
         <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
       </el-select>
      </el-form-item>
      <el-form-item label="项目:" prop="projectId">
        <el-select
          filterable
          clearable
          v-model="form.projectId"
          @change="$forceUpdate()"
          size="small"
          placeholder="-请选择项目-"
          class="ele-block">
          <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>



     <!-- <el-form-item
        label="时长:"
        prop="match_times">
          <el-select
            filterable
            clearable
            v-model="form.match_times"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择时长-"
            class="ele-block">
            <el-option v-for="item in timeList" :key="item" :label="item" :value="item"/>
          </el-select>
      </el-form-item> -->

      <el-form-item
        label="开始日期:"
        prop="begin_time">
          <el-date-picker
          type="datetime"
          class="ele-fluid"
          v-model="form.begin_time"
          value-format="yyyy-MM-dd HH:mm"
          format="yyyy-MM-dd HH:mm"
          placeholder="请选择开始时间">
          </el-date-picker>
      </el-form-item>
      
      <el-form-item
        label="结束日期:"
        prop="end_time">
      
          <el-date-picker
          type="datetime"
          class="ele-fluid"
          v-model="form.end_time"
          value-format="yyyy-MM-dd HH:mm"
          format="yyyy-MM-dd HH:mm"
          placeholder="请选择结束时间">
          </el-date-picker>
      
      </el-form-item>

      <el-form-item label="体测介绍:">
        <el-input
         :rows="3"
         clearable
         type="textarea"
         :maxlength="200"
          v-model="form.match_desc"
          placeholder="请输入体测介绍"/>
      </el-form-item>

      <el-form-item label="体测规则:">
        <el-input
         :rows="3"
         clearable
         type="textarea"
         :maxlength="200"
          v-model="form.match_rule"
          placeholder="请输入体测规则"/>
      </el-form-item>

      <el-form-item label="封面:">
        <uploadImage :limit="1" v-model="form.avatar"></uploadImage>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'MatchyxEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              avatar: [
          {required: true, message: '请输入封面', trigger: 'blur'}
        ],
                  name: [
          {required: true, message: '请输入赛事名称', trigger: 'blur'}
        ],
                  gradeId: [
          {required: true, message: '请输入年级', trigger: 'blur'}
        ],
                  projectId: [
          {required: true, message: '请输入项目ID', trigger: 'blur'}
        ],
           /*       match_times: [
          {required: true, message: '请输入时长', trigger: 'blur'}
        ], */
                  begin_time: [
          {required: true, message: '请输入开始日期', trigger: 'blur'}
        ],
                  end_time: [
          {required: true, message: '请输入结束日期', trigger: 'blur'}
        ],
                  match_desc: [
          {required: true, message: '请输入比赛介绍', trigger: 'blur'}
        ],
                  match_rule: [
          {required: true, message: '请输入比赛规则', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 项目列表
      projectList: [],
      // 年级列表
      gradeList: [],
      timeList:[],
      departmentsList:[],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
	 
	        this.getGradeList();
	        this.getProjectList();
		      
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    // 获取年级列表
  this.getGradeList();
  this.getProjectList();
  this.getTimeList();
  this.getDepartmentsList();
	 
  },
  methods: {
    /**
     * 获取组织列表
     */
    getGradeList() {
      this.$http.get('/grade/getGradeList').then(res => {
        if (res.data.code === 0) {
          this.gradeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
      * 获取院系列表
      */
      getDepartmentsList() {
       this.$http.post('/departments/getDepartmentsList').then(res => {
          if (res.data.code === 0) {
            this.departmentsList = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.$message.error(e.message);
        });
    },
    /**
     * 获取组织列表
     */
    getTimeList() {
      this.$http.get('/project/getTimeList').then(res => {
        if (res.data.code === 0) {
          this.timeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取组织列表
     */
    getProjectList() {
      this.$http.get('/project/getProjectList').then(res => {
        if (res.data.code === 0) {
          this.projectList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/matchyx/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
